import {getBanners, putbanner, getProductName} from '@/base/api/BannersApi';
import {Page} from '@/base/Page';
import {Company} from '@/base/model/Company';
import {getAgentList} from '@/base/api/AgentApi';
import {deepAssign} from '@/base/util/Objects';
import {getCompanyList} from '@/base/api/CompanyApi';
import {Agent} from '@/base/model/Agent';
import {ProductSelectorBloc} from '@/component/bloc/ProductSelectorBloc';

import {isEmpty} from '@/base/util/Strings';

export class FormBloc {
  model: any = {
    bannerId: 0,
    companyId: 0,
    agentId: 1,
    bannername: '',
    imageurl: '',
    status: 0,
    position: 1,
    actions: 1,
    linkurl: '',
    linkurlWebview: '',
    sort: 100,
    startTime: '',
    endTime: '',
    modelName: '',
    modelId: ''
  };
  loading = false;
  fileList: Array<any> = [];
  num: number = 0;
  sortList: any = [];
  companyList: any = [];
  isDetail = false;
  type: string | undefined;
  isClick = true;

  company: Page<Company> = new Page<Company>([], 0, 1, 10, 0, []);
  angent: Page<Agent> = new Page<Agent>([], 0, 1, 15, 0, []);
  ProductSelectorBloc = new ProductSelectorBloc();
  load(bannerId: number) {
    return getBanners(bannerId)
      .then((res) => {
        this.model.companyId = res.data.companyId;
        this.model.agentId = res.data.agentId;
        this.model.bannername = res.data.bannername;
        this.model.imageurl = res.data.imageurl;
        this.model.status = res.data.status;
        if (/^https:\/\//.test(res.data.linkurl)) {
          this.model.linkurl = res.data.linkurl;
        } else if(/^https:\/\//.test(res.data.linkurlWebview)){
          this.model.linkurlWebview = res.data.linkurlWebview;
        }else{
          this.model.modelId = res.data.linkurl;
        }
        this.model.actions = res.data.actions;
        this.model.sort = res.data.sort;
        this.model.startTime = res.data.startTime;
        this.model.endTime = res.data.endTime;
        if (isEmpty(this.model.linkurl)&&isEmpty(this.model.linkurlWebview)) {
          this.model.actions = 0;
        }
        // if (isEmpty(this.model.linkurl)) {
        //   this.model.actions = 0;
        // }
        return res;
      }).finally(() => {
        this.hasFormInfo = true
      })
  }
  getAgentList(pageIndex: number, pageSize: number, sortDirection: string) {
    this.loading = true;
    let that = this;
    return getAgentList({
      pageIndex,
      pageSize,
      sortDirection,
      companyId: this.model.companyId
    }).then((res) => {
      this.sortList = res.data.items.map(function (item) {
        var obj = {id: 0, agentName: ''};
        // @ts-ignore
        obj.id = item.id;
        obj.agentName = item.agentName;
        return obj;
      });
      return this.sortList;
    }).finally(() => {
      this.loading = false;
    });
  }
  getCompanyList(pageIndex: number,
                 pageSize: number,
                 sortDirection: string) {
    return getCompanyList({pageIndex, pageSize, sortDirection, name: '', createBegin: ''})
      .then((res) => {
        deepAssign(this.company, res.data);
        this.companyList = res.data.items.map(item => {
          let obj = {companyId: 0, companyName: ''};
          obj.companyId = item.id;
          obj.companyName = item.name;
          return obj
        });
        return res;
      }).finally(() => {
        this.loading = false;
      });
  }
  getProductName(modelId:number) {
    this.loading = true;
    return getProductName({modelId: modelId}).then((res) => {
      this.model.modelName = res.data;
      let a = {modelId: 0, modelName: ''};
      a.modelName = this.model.modelName;
      a.modelId = this.model.modelId;
      return res;
      }
    ).finally(
      () => {
        this.loading = false;
      }
    )
  }
  hasFormInfo = false;

  rewrite() {
    this.model.actions = 0;
  }
  formatDate(date) {
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');

    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  }
  save(model:any) {
    this.loading = true;
    let linkurl = model.linkurl;
    let linkurlWebview = model.linkurlWebview;
    let actions = model.actions;
    // if (model.modelId>0) {
    //   linkurl = model.modelId;
    //   actions = 3;
    // }
    // if(!isEmpty(model.linkurl)){
    //   actions = 2;
    // }
    // if(!isEmpty(model.linkurlWebview)){
    //   actions = 1;
    // }
    return putbanner({
      bannerId: model.bannerId,
      agentId: model.agentId,
      bannername: model.bannername,
      companyId: model.companyId,
      imageurl: model.imageurl,
      linkurl: linkurl,
      linkurlWebview: linkurlWebview,
      actions: actions,
      position: model.position,
      status: model.status,
      sort: model.sort,
      endTime: this.formatDate(model.endTime),
      startTime: this.formatDate(model.startTime)
    }).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }
}
